/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/


#include <database.h>
#include <kontamabilloader.h>
#include <QMessageBox>

#include "iesiri.h"
#include "descarcare.h"

IesiriObiecteInventar::IesiriObiecteInventar(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::iesiriObiecteInventarForm()
{
	setupUi(this);
	m_acteQry=defaultDatabase()->query("acteIesiriObiecteInventar");
	m_pozitiiQry=defaultDatabase()->query("pozitiiIesiriObiecteInventar");
	m_repereQry=defaultDatabase()->query("repereObiecteInventar");
	m_gestiuniQry=defaultDatabase()->query("gestiuniObiecteInventar");
	m_gestiunePozitieQry=defaultDatabase()->query("gestiunePozitieIesireObiecteInventar");

	connect(m_pozitiiQry,SIGNAL(beforePost()),SLOT(beforePost()));
	connect(m_pozitiiQry,SIGNAL(afterInsert(int)),SLOT(afterPost()));
	connect(m_pozitiiQry,SIGNAL(afterUpdate(int)),SLOT(afterPost()));

	connect(m_pozitiiQry, SIGNAL(stateChanged(Query::QueryState)), SLOT(pozitiiStateChanged(Query::QueryState)));

	connect(m_gestiuniQry,SIGNAL(cursorSeek(int)),SLOT(selectRepere()));

	connect(pozitii, SIGNAL(insertPressed()), SLOT(insertPozitii()));
	connect(pozitii, SIGNAL(returnPressed()), SLOT(updatePozitii()));
	connect(pozitii, SIGNAL(deletePressed()), SLOT(deletePozitii()));

	connect(adaugaButton, SIGNAL(clicked()), SLOT(insertPozitii()));
	connect(modificaButton, SIGNAL(clicked()), SLOT(updatePozitii()));
	connect(stergeButton, SIGNAL(clicked()), SLOT(deletePozitii()));

	connect(m_acteQry,SIGNAL(cursorSeek(int)),SLOT(selectPozitii()));
	ziua->setDate(QDate(kontAMabil()->globalValue("an_lucru").toInt(), kontAMabil()->globalValue("luna_lucru").toInt(),QDate::currentDate().day()));
}

void IesiriObiecteInventar::pozitiiStateChanged(Query::QueryState state)
{
	nivigatorFrame->setEnabled(state == Query::Select);
}

void IesiriObiecteInventar::on_gestiuni_returnPressed() 
{
	selectRepere();
	denumire->setFocus();
}

void IesiriObiecteInventar::on_gestiuni_escapePressed()
{
	m_pozitiiQry->cancel();
	pozitii->setFocus();
	hidePozitii();
}

void IesiriObiecteInventar::on_ziua_returnPressed()
{
	m_acteQry->bindValue( "data", ziua->date());
	m_acteQry->select();
}

void IesiriObiecteInventar::on_numarAct_returnPressed()
{
	if (!m_acteQry->size() || (m_acteQry->value("numar")!=numarAct->text() && !m_acteQry->locate("numar",numarAct->text(),Query::CaseSensitive)))
	{
		m_acteQry->insert();
		m_acteQry->setValue( "numar", numarAct->text());
		m_acteQry->setValue( "data", ziua->date());
		m_acteQry->post();
	}
	showPozitii();
	m_pozitiiQry->insert();
	actFrame->setEnabled( false );
}

void IesiriObiecteInventar::on_pozitii_escapePressed() 
{
	actFrame->setEnabled( true );
	numarAct->setFocus();
	m_pozitiiQry->close();
}


void IesiriObiecteInventar::selectPozitii()
{
	m_pozitiiQry->bindValue("id_act", m_acteQry->value("id"));
	m_pozitiiQry->select();
}
void IesiriObiecteInventar::selectRepere()
{
	m_repereQry->bindValue("id_gestiune", m_gestiuniQry->value("id"));
	m_repereQry->select();
}

void IesiriObiecteInventar::showPozitii()
{
	pozitiiFrame->setVisible( true );
	nivigatorFrame->setEnabled(false);
	gestiuni->setFocus();
}

void IesiriObiecteInventar::hidePozitii()
{
	pozitiiFrame->setVisible( false );
	layout()->activate();
	resize(QSize(width(), height() - pozitiiFrame->height()));
}

void IesiriObiecteInventar::insertPozitii()
{
	m_pozitiiQry->insert();
	showPozitii();
}

void IesiriObiecteInventar::updatePozitii()
{
	m_gestiunePozitieQry->bindValue( "id", m_pozitiiQry->value( "id" ) );
	m_gestiunePozitieQry->select();
	if (m_gestiunePozitieQry->at()<0)
	{
		m_gestiunePozitieQry->close();
		throw(tr("A intervenit o eroare la modificare"));
	}

	if (!m_gestiuniQry->locate( "id", m_gestiunePozitieQry->value( "idgestiune" ), false, false, false ))
	{
		m_gestiunePozitieQry->close();
		throw(tr("Nu gasesc gestiunea cu id:%1").arg( m_gestiunePozitieQry->value( "idgestiune" ).toString()));
	}

	selectRepere();

	if (!m_repereQry->locate( "id", m_gestiunePozitieQry->value( "idreper" ), false, false, false ))
	{
		m_pozitiiQry->cancel();
		m_gestiunePozitieQry->close();
		throw(tr("Nu gasesc reperul cu id:%1").arg( m_gestiunePozitieQry->value( "idreper" ).toString()));
	}
	m_pozitiiQry->update();
	denumire->setText( m_repereQry->value( "denumire" ).toString() );
	m_gestiunePozitieQry->close();
	showPozitii();
	denumire->setFocus();
}

void IesiriObiecteInventar::deletePozitii() 
{
	if (QMessageBox::question(this, tr("Pozitii document"), tr("Doriti sa stergeti pozitia curenta ?"), QMessageBox::Yes|QMessageBox::No)!=QMessageBox::Yes)
		return;
	m_pozitiiQry->remove();
	pozitii->setFocus();
}

void IesiriObiecteInventar::showEvent ( QShowEvent * event )
{
	hidePozitii();
	KontAMabilWindow::showEvent(event);
	ziua->setFocus();
}

void IesiriObiecteInventar::beforePost()
{
	m_pozitiiQry->setValue( "id_act", m_acteQry->value("id"));
	m_pozitiiQry->setValue( "data", ziua->date());
}

void IesiriObiecteInventar::on_cantitate_returnPressed()
{
	m_pozitiiQry->post();
	m_pozitiiQry->insert();
	denumire->setFocus();
}

void IesiriObiecteInventar::afterPost()
{
	DescarcareObiecteInventar d;
	d.exec();
}

IesiriObiecteInventar::~IesiriObiecteInventar()
{
}
